/**
 * 
 */
package de.nak.fireDepartment.service;

import java.util.Collection;

import org.apache.log4j.Logger;

import de.nak.fireDepartment.dao.UserDAO;
import de.nak.fireDepartment.logger.MyLog4JLogger;
import de.nak.fireDepartment.model.User;

/**
 * Implementation of the Login Interface
 * @author Sven Redelin
 *
 */
public class LoginServiceImpl implements LoginService {

	Logger logger;
	
	/**
	 * The Contructor gets the Logger and creates a Message
	 */
	public LoginServiceImpl(){
		logger = MyLog4JLogger.getLogger();
		logger.info("Instance of LoginServiceImpl has been created");
	}
	
	/**
	 * The User data access object
	 */
	private UserDAO userDAO;
	
	/**
	 *  The currently logged in users
	 */
	private User currentUser = null;

	/** {@inheritDoc} */
	public Collection<User> listUsers() {
		logger.info("List all Users");
		return userDAO.findAll();
	}
	
	/** {@inheritDoc} */
	public boolean validateLogin(User user){
		// get all Users
		Collection<User> users = userDAO.findAll();
		
		// check login data
		for (User aUser : users) {
			if(user.equals(aUser)){
				currentUser = aUser;
				logger.info("Login correct. User logged in");
				return true;
			}
		}
		logger.info("Login incorrect");
		return false;
	}
	
	/** {@inheritDoc} */
	public boolean checkUserIsValidated(){
		// if currentUser is null the user isn't logged in.
		if(currentUser == null){
			logger.info("User isn't logged in");
			return false;
		}
		logger.info("User is logged in");
		return true;
	}
	
	/** {@inheritDoc} */
	public void logoutUser(){
		// delete the current user
		currentUser = null;
		logger.info("User is logged out");
	}

	/**
	 * Get userDAO
	 * @return the userDAO
	 */
	public UserDAO getUserDAO() {
		return userDAO;
	}

	/**
	 * Set userDAO
	 * @param userDAO the userDAO to set
	 */
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

}
